import BasePresenter from "../../../../../../redare/presenter/BasePresenter";
import * as UserHttp from "../../../http/component/core/UserHttp";

/**
 * Created by RdevPlugins.JsPresenterBuilderAction on 2020-03-07 13:02:40.
 */

export default class UserPresenter extends BasePresenter {

    /**
     * @param body: {mobile, email, name, id, account, key, platform, userPlatform, menuId, needResetPwd, roleIds, status, incMenu, incRole, incRoot, incPwd}
     * @param query: {curPage,pageSize}
     * @returns Promise
     */
    findUserPage(body, query, success, fails) {

        if (this._onBeforeFindUserPage) {
            let data = this._onBeforeFindUserPage(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.findUserPage(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindUserPageSuccess) {
                    result = this._onBeforeFindUserPageSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindUserPageSuccess) {
                    this.view._onFindUserPageSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {mobile, email, name, id, account, key, platform, userPlatform, menuId, needResetPwd, roleIds, status, incMenu, incRole, incRoot, incPwd}
     * @param query: {returnAll,curPage,pageSize}
     * @returns Promise
     */
    findUserList(body, query, success, fails) {

        if (this._onBeforeFindUserList) {
            let data = this._onBeforeFindUserList(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.findUserList(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindUserListSuccess) {
                    result = this._onBeforeFindUserListSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindUserListSuccess) {
                    this.view._onFindUserListSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, account, userPlatform, platform, status, incMenu, incPwd}
     * @param query: {}
     * @returns Promise
     */
    getUser(body, query, success, fails) {

        if (this._onBeforeGetUser) {
            let data = this._onBeforeGetUser(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.getUser(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeGetUserSuccess) {
                    result = this._onBeforeGetUserSuccess(result, {body, query});
                }

                if (this.view && this.view._onGetUserSuccess) {
                    this.view._onGetUserSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {name, mobile, email, photo, account, pwd, platform, userPlatform, menuId, roleIds, needResetPwd}
     * @param query: {}
     * @returns Promise
     */
    addUser(body, query, success, fails) {

        if (this._onBeforeAddUser) {
            let data = this._onBeforeAddUser(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.addUser(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeAddUserSuccess) {
                    result = this._onBeforeAddUserSuccess(result, {body, query});
                }

                if (this.view && this.view._onAddUserSuccess) {
                    this.view._onAddUserSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {account, name, mobile, email, photo, where: {platform, userPlatform, userId}}
     * @param query: {}
     * @returns Promise
     */
    modifyUser(body, query, success, fails) {

        if (this._onBeforeModifyUser) {
            let data = this._onBeforeModifyUser(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.modifyUser(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeModifyUserSuccess) {
                    result = this._onBeforeModifyUserSuccess(result, {body, query});
                }

                if (this.view && this.view._onModifyUserSuccess) {
                    this.view._onModifyUserSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {accountId, platform, userPlatform, pwd, needResetPwd}
     * @param query: {}
     * @returns Promise
     */
    modifyPwd(body, query, success, fails) {

        if (this._onBeforeModifyPwd) {
            let data = this._onBeforeModifyPwd(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.modifyPwd(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeModifyPwdSuccess) {
                    result = this._onBeforeModifyPwdSuccess(result, {body, query});
                }

                if (this.view && this.view._onModifyPwdSuccess) {
                    this.view._onModifyPwdSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {accountId, platform, userPlatform, menuId}
     * @param query: {}
     * @returns Promise
     */
    modifyMenu(body, query, success, fails) {

        if (this._onBeforeModifyMenu) {
            let data = this._onBeforeModifyMenu(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.modifyMenu(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeModifyMenuSuccess) {
                    result = this._onBeforeModifyMenuSuccess(result, {body, query});
                }

                if (this.view && this.view._onModifyMenuSuccess) {
                    this.view._onModifyMenuSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {accountId, roleIds, platform, userPlatform, replace}
     * @param query: {}
     * @returns Promise
     */
    modifyRole(body, query, success, fails) {

        if (this._onBeforeModifyRole) {
            let data = this._onBeforeModifyRole(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.modifyRole(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeModifyRoleSuccess) {
                    result = this._onBeforeModifyRoleSuccess(result, {body, query});
                }

                if (this.view && this.view._onModifyRoleSuccess) {
                    this.view._onModifyRoleSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {accountId, platform, userPlatform, status, statusMsg}
     * @param query: {}
     * @returns Promise
     */
    modifyStatus(body, query, success, fails) {

        if (this._onBeforeModifyStatus) {
            let data = this._onBeforeModifyStatus(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.modifyStatus(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeModifyStatusSuccess) {
                    result = this._onBeforeModifyStatusSuccess(result, {body, query});
                }

                if (this.view && this.view._onModifyStatusSuccess) {
                    this.view._onModifyStatusSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {accountId, platform, userPlatform, roleIds, menuId}
     * @param query: {}
     * @returns Promise
     */
    modifyPlatform(body, query, success, fails) {

        if (this._onBeforeModifyPlatform) {
            let data = this._onBeforeModifyPlatform(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.modifyPlatform(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeModifyPlatformSuccess) {
                    result = this._onBeforeModifyPlatformSuccess(result, {body, query});
                }

                if (this.view && this.view._onModifyPlatformSuccess) {
                    this.view._onModifyPlatformSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {accountId, userPlatform, roleIds}
     * @param query: {}
     * @returns Promise
     */
    modifyUserPlatform(body, query, success, fails) {

        if (this._onBeforeModifyUserPlatform) {
            let data = this._onBeforeModifyUserPlatform(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.modifyUserPlatform(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeModifyUserPlatformSuccess) {
                    result = this._onBeforeModifyUserPlatformSuccess(result, {body, query});
                }

                if (this.view && this.view._onModifyUserPlatformSuccess) {
                    this.view._onModifyUserPlatformSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {accountId, account, platform, userPlatform}
     * @param query: {}
     * @returns Promise
     */
    delUser(body, query, success, fails) {

        if (this._onBeforeDelUser) {
            let data = this._onBeforeDelUser(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        UserHttp.delUser(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeDelUserSuccess) {
                    result = this._onBeforeDelUserSuccess(result, {body, query});
                }

                if (this.view && this.view._onDelUserSuccess) {
                    this.view._onDelUserSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

}
